<?php
/**
 * BootAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using BootAdmin.
 *
 * @Author G.r<root@BootAdmin.com>
 */

declare (strict_types=1);

namespace app\admin\model;

use app\common\model\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 角色模型
 */
class Role extends BaseModel
{
    // 定义软删除
    use SoftDelete;

    protected array $allowModifyFields = ['data_scope', 'status', 'sort'];

    protected array $fields = ['id', 'id' => 'value', 'name' => 'label'];

    // 所有
    public const ALL_SCOPE = 1;
    // 自定义
    public const CUSTOM_SCOPE = 2;
    // 本部门
    public const SELF_DEPT_SCOPE = 3;
    // 本部门及子部门
    public const DEPT_BELOW_SCOPE = 4;
    // 本人
    public const SELF_SCOPE = 5;

    /**
     * 初始化
     */
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->query->order('sort', 'desc');
    }

    /**
     * 通过中间表获取菜单
     */
    public function menus()
    {
        return $this->belongsToMany(Menu::class, 'role_menu', 'menu_id', 'role_id');
    }
    /**
     * 通过中间表获取用户
     */
    public function admins()
    {
        return $this->belongsToMany(Admin::class, 'user_role', 'admin_id', 'role_id');
    }
    /**
     * 通过中间表获取部门
     */
    public function depts()
    {
        return $this->belongsToMany(Dept::class, 'role_dept', 'dept_id', 'role_id');
    }
}